route.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import NextAuth from "next-auth";
  2. import CredentialsProvider from "next-auth/providers/credentials";
  3. import * as R from "ramda";
  4. const authOptions = {
  5. // https://generate-secret.vercel.app/32
  6. secret: process.env.SECRET,
  7. // Configure one or more authentication providers
  8. providers: [
  9. CredentialsProvider({
  10. // The name to display on the sign in form (e.g. 'Sign in with...')
  11. name: "Cocorobo cloud",
  12. // The credentials is used to generate a suitable form on the sign in page.
  13. // You can specify whatever fields you are expecting to be submitted.
  14. // e.g. domain, username, password, 2FA token, etc.
  15. // You can pass any HTML attribute to the <input> tag through the object.
  16. credentials: {
  17. userId: { label: "theUserId", type: "text", required: true },
  18. // loginUsername: { label: "用户名", type: "text" },
  19. // loginPassword: { label: "密码", type: "password" },
  20. },
  21. async authorize(credentials, req) {
  22. return { id: credentials.userId, name: 'anonymous' };
  23. // You need to provide your own logic here that takes the credentials
  24. // submitted and returns either a object representing a user or value
  25. // that is false/null if the credentials are invalid.
  26. // e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
  27. // You can also use the `req` object to obtain additional parameters
  28. // (i.e., the request IP address)
  29. /*
  30. const res = await fetch("https://beta.api.cocorobo.cn/api/user", {
  31. method: "POST",
  32. body: JSON.stringify(
  33. R.pick(["loginUsername", "loginPassword"], credentials)
  34. ),
  35. headers: {
  36. "Content-Type": "application/json",
  37. Origin: "https://edu.cocorobo.cn",
  38. },
  39. });
  40. if (res.status !== 200) {
  41. return null;
  42. }
  43. const resJson = await res.json();
  44. const user = resJson?.[0]?.[0];
  45. // If no error and we have user data, return it
  46. if (res.ok && user && user.active) {
  47. return { ...user, id: user.userid, name: user.username };
  48. }
  49. */
  50. },
  51. }),
  52. ],
  53. callbacks: {
  54. // we have no db intergrate, `user` is always empty because there is no db record
  55. async session({ session, token, user: _user }) {
  56. // Send properties to the client, like an access_token from a provider.
  57. session.user.id = token.sub
  58. try {
  59. const res = await fetch(
  60. `https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${token.sub}`,
  61. {
  62. method: "GET",
  63. headers: {
  64. "Content-Type": "application/json",
  65. },
  66. }
  67. );
  68. const username = (await res.json())?.[0]?.[0]?.username;
  69. session.user.name = username;
  70. } catch (e) {
  71. session.user.name = token.name
  72. }
  73. return session;
  74. },
  75. },
  76. };
  77. const handler = NextAuth(authOptions);
  78. export { handler as GET, handler as POST };